package algorithm.swordoff;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * 最小的k个数
 */

public class SQ40 {

    public int[] getLeastNumbers(int[] arr, int k) {
        if (k == 0) return new int[0];

        PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2-o1);

        for (int i : arr) {
            if (pq.size() < k) pq.add(i);
            else {
                if (pq.peek() > i) {
                    pq.poll();
                    pq.add(i);
                }
            }
        }

        int[] ans = new int[k];

        for (int i = 0; i < ans.length; i++) ans[i] = pq.poll();
        return ans;
    }

    public static void main(String[] args) {
        SQ40 sq40 = new SQ40();
        System.out.println(Arrays.toString(sq40.getLeastNumbers(new int[]{}, 0)));
    }


}
